import lodash from 'lodash';
import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

const likeParms = 'name'.split(',');
export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch productimages params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.fetch(where, 1, pageOpt).then(res => {
            log.info('fetch productimages result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch productimages is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find productimages params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find productimages result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find productimages is err----', err);
        return err;
    });
}
export function add(productimages) {
    return util.resolve().then(() => {
        log.info('add productimages params----', productimages);
        productimages.id = util.uuid();
        return data.add(productimages).then(res => {
            log.info('add productimages result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('add productimages is err----', err);
        return err;
    });
}
export function edit(productimages) {
    return util.resolve().then(() => {
        log.info('edit productimages params----', productimages);
        return data.edit(productimages).then(res => {
            log.info('edit productimages result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('edit productimages is err----', err);
        return err;
    });
}
// 设置主图
export function isMain(info) {
    return util.resolve().then(() => {
        log.info('isMain productimages params----', info);
        const {id, productId} = info;
        //获取原有的主图数据
        return data.find({productId}).then(r => {
            r = r.toJSON() || [];
            if (r.length > 0 && r.id !== id) {
                r.isMain = 0;
                data.edit(r);
            }
        }).then(() => {
            info.isMain = 1;
            data.edit(info).then(res => {
                log.info('isMain productimages result----', util.data(res));
                return util.data(res);
            });
        });
    }).catch(err => {
        log.error('edit productimages is err----', err);
        throw err;
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete productimages params----', id);
        return data.del(id).then(res => {
            log.info('delete productimages result----success', util.data(res));
            //TODO:删除图片
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete productimages is err----', err);
        return err;
    });
}
